package com.edgclub.edg.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.edgclub.edg.R;
import com.edgclub.edg.activity.GzTopicSendActivity;
import com.edgclub.edg.constant.BBConstant;
import com.edgclub.edg.event.BroadCastEvent;
import com.edgclub.edg.event.EventBus;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.squareup.otto.ThreadEnforcer;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;


public class EDGHomeFragment extends GzBaseFragment implements View.OnClickListener{
	private String classname = "EDGHomeFragment";
	private ViewPager mViewPager;
	private ArrayList<Fragment> fragmentList;

	private FrameLayout ll_tab1,ll_tab2,ll_tab3,ll_tab4;
	private ImageView iv_1,iv_2,iv_3,iv_4,btn_write;

	private BBFragment edgFragment;
	private EDGNewsFragment edgNewsFragment;
	private EDGActivityFragment edgActivityFragment;
	private EDGTeamFragment edgTeamFragment;

	private Activity mActivity;
	private UserPrefUtils userPrefUtils;


	public static EDGHomeFragment newInstance(String text) {
		EDGHomeFragment f = new EDGHomeFragment();
		return f;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mActivity = this.getActivity();
		return inflater.inflate(R.layout.fragment_edg_home, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		initView(view);
	}

	public void initView(View view){
		btn_write = (ImageView) getView().findViewById(R.id.btn_write);
		ll_tab1 = (FrameLayout) view.findViewById(R.id.ll_tab1);
		ll_tab2 = (FrameLayout) view.findViewById(R.id.ll_tab2);
		ll_tab3 = (FrameLayout) view.findViewById(R.id.ll_tab3);
		ll_tab4 = (FrameLayout) view.findViewById(R.id.ll_tab4);
		iv_1 = (ImageView) view.findViewById(R.id.iv_1);
		iv_2 = (ImageView) view.findViewById(R.id.iv_2);
		iv_3 = (ImageView) view.findViewById(R.id.iv_3);
		iv_4 = (ImageView) view.findViewById(R.id.iv_4);
		ll_tab1.setOnClickListener(this);
		ll_tab2.setOnClickListener(this);
		ll_tab3.setOnClickListener(this);
		ll_tab4.setOnClickListener(this);
		btn_write.setOnClickListener(this);

		userPrefUtils = new UserPrefUtils(getActivity());
		if("0".equals(userPrefUtils.getUser().getUserAuth().getType())){
			btn_write.setVisibility(View.VISIBLE);
		}else
		{
			btn_write.setVisibility(View.GONE);
		}

		mViewPager = (ViewPager)getView().findViewById(R.id.viewpager_edg);
		mViewPager.setOffscreenPageLimit(3);
		fragmentList = new ArrayList<Fragment>();
		edgFragment= BBFragment.newInstance(BBConstant.TOPIC_TYPE_EDG,null);

		edgNewsFragment = new EDGNewsFragment();
		edgActivityFragment = new EDGActivityFragment();
		edgTeamFragment = new EDGTeamFragment();
		fragmentList.add(edgFragment);
		fragmentList.add(edgNewsFragment);
		fragmentList.add(edgActivityFragment);
		fragmentList.add(edgTeamFragment);
		//给ViewPager设置适配器
		mViewPager.setAdapter(new MyFragmentPagerAdapter(this.getChildFragmentManager(), fragmentList));
		mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());//页面变化时的监听器
		mViewPager.setCurrentItem(0);//设置当前显示标签页为第一页
		switchtab(ll_tab1,iv_1);

		EventBus.getEventBus(BroadCastEvent.EVENTBUS_COMMON,
				ThreadEnforcer.MAIN).register(this);
	}

	@Override
	public void onHiddenChanged(boolean hidd) {
		if (hidd) {

		} else {
			//相当于Fragment的onPause
			if(null!=mViewPager)
				mViewPager.setCurrentItem(mViewPager.getCurrentItem());
			switch (mViewPager.getCurrentItem()){
				case 0:
					switchtab(ll_tab1,iv_1);
					break;
				case 1:
					switchtab(ll_tab2,iv_2);
					break;
				case 2:
					switchtab(ll_tab3,iv_3);
					break;
				case 3:
					switchtab(ll_tab4,iv_4);
					break;
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()){
//			case R.id.publishposts:
//				// 发布贴子
//				GuZhiApplication.getInstance().setClassname(classname);
//				Intent intent = new Intent(getActivity(),
//						GZTopicLaunchActivity.class);
//				getActivity().startActivity(intent);
//				break;
			case R.id.ll_tab1:
				switchtab(ll_tab1,iv_1);
				mViewPager.setCurrentItem(0);
				break;
			case R.id.ll_tab2:
				switchtab(ll_tab2,iv_2);
				mViewPager.setCurrentItem(1);
				break;
			case R.id.ll_tab3:
				switchtab(ll_tab3,iv_3);
				mViewPager.setCurrentItem(2);
				break;
			case R.id.ll_tab4:
				switchtab(ll_tab4,iv_4);
				mViewPager.setCurrentItem(3);
				break;
			case R.id.btn_write:
				Intent intent = new Intent(mActivity, GzTopicSendActivity.class);
				mActivity.startActivity(intent);
				break;

		}
	}


	public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onPageSelected(int page) {
			// TODO Auto-generated method stub
			switch (page){
				case 0:
					switchtab(ll_tab1,iv_1);
					MobclickAgent.onEvent(getActivity(),"EC_EDG_FLOW");
					break;
				case 1:
					switchtab(ll_tab2,iv_2);
					MobclickAgent.onEvent(getActivity(),"EC_EDG_INFORMATION");
					break;
				case 2:
					switchtab(ll_tab3,iv_3);
					MobclickAgent.onEvent(getActivity(),"EC_EDG_ACTIVITY");
					break;
				case 3:
					switchtab(ll_tab4,iv_4);
					MobclickAgent.onEvent(getActivity(),"EC_EDG_TEAM_MEMBER");
					break;
			}
		}
	}

	private void switchtab(FrameLayout ll_tab,ImageView iv) {
		ll_tab1.setSelected(false);
		ll_tab2.setSelected(false);
		ll_tab3.setSelected(false);
		ll_tab4.setSelected(false);
		iv_1.setVisibility(View.GONE);
		iv_2.setVisibility(View.GONE);
		iv_3.setVisibility(View.GONE);
		iv_4.setVisibility(View.GONE);
		ll_tab.setSelected(true);
		iv.setVisibility(View.VISIBLE);
	}


	class MyFragmentPagerAdapter extends FragmentPagerAdapter {
		ArrayList<Fragment> list;

		public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> list) {
			super(fm);
			this.list = list;
		}

		@Override
		public Fragment getItem(int position) {
			return list.get(position);
		}

		@Override
		public int getCount() {
			return list.size();
		}
	}


	@Override
	public void onDestroy() {
		super.onDestroy();
		EventBus.getEventBus(BroadCastEvent.EVENTBUS_COMMON,
				ThreadEnforcer.MAIN).unregister(this);
	}
}

